/**
 * @author Stephan Pfaff (http://www.derdaumen.net)
 */

/**
 * The Button Object
 * @param vars object providing the button properties (loaded from the database)
 * @return
 */
function Button(vars, params, parent){
	if(vars != null) {
		try {
			Item.call(this, vars.prefix, vars.id, vars.type, vars.basename, vars.path, vars.label, vars.alias, vars.thumb, vars.thumb_enable, false, vars.item_class);
			this.category_id 	= parseInt(vars.category_id);
			this.project_id 	= parseInt(vars.project_id);
			if (vars.img != null && vars.img != '')
				this.img 		= vars.img;
			this.img_title		= vars.img_title;
			this.img_enable 	= vars.img_enable;
			this.lightbox_enable = vars.lightbox_enable;
			var img_rel = this.lightbox_enable ? 'lightbox['+this.prefix+']' : '';
			this.x				= parseInt(vars.x*site.gridSize);
			this.y				= parseInt(vars.y*site.gridSize);
			this.content_x		= parseInt(vars.content_x*site.gridSize);
			this.content_y		= parseInt(vars.content_y*site.gridSize);
			this.content_width	= parseInt((vars.content_width*site.gridSize)-7);
			this.content_height	= parseInt((vars.content_height*site.gridSize)-7);
			this.interactive	= vars.interactive;
			this.text			= vars.text;
			this.text_enable	= vars.text_enable;
			this.link			= vars.link;
			this.link_enable	= vars.link_enable;
			this.img_delete 	= false;

			this.div_id = this.prefix+"_"+this.id;
			var class_active = this.interactive ? '' : 'active';
			this.div_class = 'item '+this.prefix+' '+this.item_class+' '+class_active;
			this.div_x = this.interactive ? "left: "+this.x+"px;" : "left: "+this.content_x+"px;";
			this.div_y = this.interactive ? "top: "+this.y+"px;" : "top: "+this.content_y+"px;";
			this.div_width = this.interactive ? "" : "width: "+this.content_width+"px;";
			this.div_height = this.interactive ? "" : " height: "+this.content_height+"px;";
			this.bg_active = '#232323';
			if(this.label != "" || this.thumb != "")
				this.bg_inactive = '#232323';
			else
				this.bg_inactive = '#999';
			this.bg = this.interactive ? this.bg_inactive : this.bg_active;
			if(this.img_enable && this.img != "") var textClass = 'caption';
			else var textClass = 'text';
			
			//Externer Link nur erstellen wenn keine Lightbox aktiviert ist, 
			//oder falls lightbox aktiv ist, dann nur wenn dieser button nicht der aktive button ist
			var external_link = true;
			if (this.link != null) {
				if (external_link = this.link_enable) {
					if (this.lightbox_enable) {
						external_link = false;
					}
				}
			} else {
				external_link = false;
			}
			if(this.img != '') {
				var filetype = this.img.substring(this.img.lastIndexOf('.'));
				var filename = this.img.substring(0, this.img.lastIndexOf('.'));
				var preview = filename+'_preview'+filetype;
			}
			
			if(noPHPMode) {
				var html = ["<div id='"+this.div_id+"' class='"+this.div_class+"' style='"+this.div_x+" "+this.div_y+" "+this.div_width+" "+this.div_height+" background:"+this.bg+";'>\n"];
					if(this.thumb != "" && this.thumb_enable)		 				html.push("	<img id='thumb_"+this.div_id+"' class='thumb' src='"+this.thumb+"' alt='"+this.alias+"' width='73' height='73' />\n");
					if(this.label != "") 											html.push("	<div id='label_"+this.div_id+"' class='label'>"+this.label+"</div>\n");
					if(this.img != "" && this.img_enable && !this.lightbox_enable)	html.push("	<img id='img_"+this.div_id+"' class='img' src='"+preview+"' alt='"+this.img_title+"' title='"+this.img_title+"' width='"+this.content_width+"' height='"+this.content_height+"' />\n");
					if(this.img != "" && this.img_enable && this.lightbox_enable) 	html.push("	<a href='"+this.img+"' rel='"+img_rel+"' title='"+this.img_title+"' class='lightbox' target='blank'><img id='img_"+this.div_id+"' class='img' src='"+preview+"' alt='"+this.img_title+"' width='"+this.content_width+"' height='"+this.content_height+"' /></a>\n");
					if(this.text != "" && this.text_enable) 						html.push("	<div id='text_"+this.div_id+"' class='"+textClass+"'>\n"+this.text+"\n</div>\n");
					if(external_link)		 										html.push("	<a id='outerlink_"+this.div_id+"' class='link nav outer' href='"+this.link+"' target='_blank'></a>\n");
					html.push("</div>\n");
					this.$div = $(html.join(''));
					parent.$scrollWrapper.append(this.$div);
			} else {
				this.$div = $("#"+this.prefix+"_"+this.id);
			}
			if(noPHPMode && allowDraggable) {
				this.$div.draggable({
					revert: false,
					cancel: '.active',
					zIndex: 999
				});
			}
			//durch animationen ersetzte lins zum umschalten der buttons 
			//in der php Version entfernen
			$("#innerlink_"+this.div_id).remove();
			
			var handle = this;
			//hover
			this.$div.hover(function(event) {
				handle.activate($(this), event);
			},
			function(event) {
				handle.deactivate($(this), event);
			});
			//click
			this.$div.click(function(event) {
				handle.clickHandler($(this), event);
			});
		} catch (e) {
			if(debug) alert('Initialization of Button failed, missing or wrong parameter! ('+e+')');
		}
	}
};
Button.prototype = new Item();
Button.prototype.category_id = 0;
Button.prototype.project_id = 0;
Button.prototype.x = 0;
Button.prototype.y = 0;
Button.prototype.content_x = 0;
Button.prototype.content_y = 0;
Button.prototype.content_width = 1;
Button.prototype.content_height = 1;
Button.prototype.interactive = 0;
Button.prototype.link = "";
Button.prototype.link_enable = false;
Button.prototype.text = "";
Button.prototype.text_enable = false;
Button.prototype.img = "";
Button.prototype.img_title = "";
Button.prototype.img_enable = false;
Button.prototype.lightbox_enable = false;
Button.prototype.img_delete = false;
Button.prototype.html = "";
Button.prototype.$div = null;
Button.prototype.div_id = null;
Button.prototype.div_class = null;
Button.prototype.div_x = null;
Button.prototype.div_y = null;
Button.prototype.div_width = null;
Button.prototype.div_height = null;
Button.prototype.bg_active = null;
Button.prototype.bg_inactive = null;
Button.prototype.bg = null;

Button.prototype.activate = function($div, event) {
	var div_id = this.div_id;
	if(!this.locked && this.interactive == true) {
		$div.clearQueue()
		.stop()
		.css('z-index', 103)
		.queue(function() {
			var $label = $("#label_"+div_id);
			var $thumb = $("#thumb_"+div_id);
			if($label.length > 0) {
				$thumb.fadeOut(site.btnAnimationDuration);
				$label.fadeOut(site.btnAnimationDuration, function() {
					$div.dequeue();
				});
			} else if($thumb.length > 0) {
				$label.fadeOut(site.btnAnimationDuration);
				$thumb.fadeOut(site.btnAnimationDuration, function() {
					$div.dequeue();
				});
			} else {
				$div.dequeue();
			}
		})
		.addClass("active", site.btnAnimationDuration)
		.animate({
			backgroundColor: this.bg_active,
			left: this.content_x,
			top: this.content_y,
			width: this.content_width,
			height: this.content_height
		}, site.btnAnimationDuration, function() {
			$("#img_"+div_id).fadeIn(site.btnAnimationDuration);
			$("#text_"+div_id).fadeIn(site.btnAnimationDuration);
		});
	}
};
Button.prototype.deactivate = function($div, event) {
	var div_id = this.div_id;
	if(!this.locked && this.interactive == true) {
		$div.clearQueue()
		.stop()
		.css('z-index', 102)
		.queue(function() {
			var $img = $("#img_"+div_id);
			var $text = $("#text_"+div_id);
			if($img.length > 0) {
				$text.fadeOut(site.btnAnimationDuration/2);
				$img.fadeOut(site.btnAnimationDuration/2, function() {
					$div.dequeue();
				});
			} else if($text.length > 0) {
				$img.fadeOut(site.btnAnimationDuration/2);
				$text.fadeOut(site.btnAnimationDuration/2, function() {
					$div.dequeue();
				});
			} else {
				$div.dequeue();
			}
		})
		.removeClass("active", site.btnAnimationDuration)
		.animate({
			backgroundColor: this.bg_inactive,
			left: this.x,
			top: this.y,
			width: 73,
			height: 73
		}, site.btnAnimationDuration, function() {
			$div.css('z-index', 0);
			$("#label_"+div_id).fadeIn(site.btnAnimationDuration);
			$("#thumb_"+div_id).fadeIn(site.btnAnimationDuration);
		});
	}
};
Button.prototype.clickHandler = function($div, event) {
	/*if($div.hasClass('active')) {
		this.deactivate($div, event);
	} else {
		this.activate($div, event);
	}*/
};